package top.zywork.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.zywork.annotation.SysLog;
import top.zywork.common.BeanUtils;
import top.zywork.common.BindingResultUtils;
import top.zywork.common.StringUtils;
import top.zywork.dto.PagerDTO;
import top.zywork.dto.GraphicWalletDetailsDTO;
import top.zywork.ext.dos.GraphicExtDO;
import top.zywork.ext.service.GraphicExtService;
import top.zywork.ext.service.GraphicWalletDetailsExtService;
import top.zywork.query.GraphicWalletDetailsQuery;
import top.zywork.security.SecurityUtils;
import top.zywork.service.GraphicWalletDetailsService;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.vo.PagerVO;
import top.zywork.vo.GraphicWalletDetailsVO;

import java.util.List;
import java.util.stream.Collectors;

/**
 * GraphicWalletDetailsController控制器类<br/>
 *
 * 创建于2020-11-30<br/>
 *
 * @author http://ljyanimo.com 刘金泳
 * @version 1.0
 */
@RestController
@RequestMapping("/graphic-wallet-details")
@Slf4j
public class GraphicWalletDetailsController extends BaseController {

    private GraphicWalletDetailsService graphicWalletDetailsService;

    @Autowired
    private GraphicWalletDetailsExtService graphicWalletDetailsExtService;

    @Autowired
    private GraphicExtService graphicExtService;

    @PostMapping("admin/save")
    @SysLog(description = "后台保存GraphicWalletDetailsVO")
    public ResponseStatusVO save(@RequestBody @Validated GraphicWalletDetailsVO graphicWalletDetailsVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        graphicWalletDetailsService.save(BeanUtils.copy(graphicWalletDetailsVO, GraphicWalletDetailsDTO.class));
        return ResponseStatusVO.ok("添加成功", null);
    }

    @PostMapping("admin/batch-save")
    @SysLog(description = "后台批量保存GraphicWalletDetailsVO")
    public ResponseStatusVO saveBatch(@RequestBody @Validated List<GraphicWalletDetailsVO> graphicWalletDetailsVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        graphicWalletDetailsService.saveBatch(BeanUtils.copyListObj(graphicWalletDetailsVOList, GraphicWalletDetailsDTO.class));
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @GetMapping("admin/remove/{id}")
    @SysLog(description = "后台删除GraphicWalletDetailsVO")
    public ResponseStatusVO removeById(@PathVariable("id") Long id) {
        graphicWalletDetailsService.removeById(id);
        return ResponseStatusVO.ok("删除成功", null);
    }

    @PostMapping("admin/batch-remove")
    @SysLog(description = "后台批量删除GraphicWalletDetailsVO")
    public ResponseStatusVO removeByIds(@RequestBody String[] ids) {
        graphicWalletDetailsService.removeByIds(StringUtils.strArrayToLongArray(ids));
        return ResponseStatusVO.ok("批量删除成功", null);
    }

    @PostMapping("admin/update")
    @SysLog(description = "后台更新GraphicWalletDetailsVO")
    public ResponseStatusVO update(@RequestBody @Validated GraphicWalletDetailsVO graphicWalletDetailsVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        int updateRows = graphicWalletDetailsService.update(BeanUtils.copy(graphicWalletDetailsVO, GraphicWalletDetailsDTO.class));
        if (updateRows == 1) {
            return ResponseStatusVO.ok("更新成功", null);
        } else {
            return ResponseStatusVO.dataError("数据版本号有问题，在此更新前数据已被更新", null);
        }
    }

    @PostMapping("admin/batch-update")
    @SysLog(description = "后台批量更新GraphicWalletDetailsVO")
    public ResponseStatusVO updateBatch(@RequestBody @Validated List<GraphicWalletDetailsVO> graphicWalletDetailsVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        graphicWalletDetailsService.updateBatch(BeanUtils.copyListObj(graphicWalletDetailsVOList, GraphicWalletDetailsDTO.class));
        return ResponseStatusVO.ok("批量更新成功", null);
    }

    @PostMapping("admin/active")
    @SysLog(description = "后台激活或冻结GraphicWalletDetailsVO")
    public ResponseStatusVO active(@RequestBody GraphicWalletDetailsVO graphicWalletDetailsVO) {
        graphicWalletDetailsService.update(BeanUtils.copy(graphicWalletDetailsVO, GraphicWalletDetailsDTO.class));
        return ResponseStatusVO.ok("激活或冻结成功", null);
    }

    @PostMapping("admin/batch-active")
    @SysLog(description = "后台批量激活或冻结GraphicWalletDetailsVO")
    public ResponseStatusVO activeBatch(@RequestBody @Validated List<GraphicWalletDetailsVO> graphicWalletDetailsVOList) {
        graphicWalletDetailsService.updateBatch(BeanUtils.copyListObj(graphicWalletDetailsVOList, GraphicWalletDetailsDTO.class));
        return ResponseStatusVO.ok("批量激活或冻结成功", null);
    }

    @GetMapping("admin/one/{id}")
    public ResponseStatusVO getById(@PathVariable("id") Long id) {
        GraphicWalletDetailsVO graphicWalletDetailsVO = new GraphicWalletDetailsVO();
        Object obj = graphicWalletDetailsService.getById(id);
        if (obj != null) {
            graphicWalletDetailsVO = BeanUtils.copy(obj, GraphicWalletDetailsVO.class);
        }
        return ResponseStatusVO.ok("查询成功", graphicWalletDetailsVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        PagerDTO pagerDTO = graphicWalletDetailsService.listAll();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GraphicWalletDetailsVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody GraphicWalletDetailsQuery graphicWalletDetailsQuery) {
        PagerDTO pagerDTO = graphicWalletDetailsService.listAllByCondition(graphicWalletDetailsQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), GraphicWalletDetailsVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody GraphicWalletDetailsQuery graphicWalletDetailsQuery) {

        QueryWrapper queryWrapper = new QueryWrapper();

        long userId = SecurityUtils.getJwtUser().getUserId();

        List<GraphicExtDO> user_id = graphicExtService.list(new QueryWrapper<GraphicExtDO>().eq("user_id", userId));

        // 图文店
        if(user_id.size() > 0){
            List<Integer> ids;
            ids = user_id.stream().map(e->e.getId()).collect(Collectors.toList());
            queryWrapper.in("graphic_id",ids);
        }

        IPage page = graphicWalletDetailsExtService.page(new Page<>(graphicWalletDetailsQuery.getPageNo(), graphicWalletDetailsQuery.getPageSize()),queryWrapper);
        PagerDTO pagerDTO = new PagerDTO();
        pagerDTO.setTotal(page.getTotal());
        pagerDTO.setRows(page.getRecords());

        return ResponseStatusVO.ok("查询成功", pagerDTO);
    }

    @Autowired
    public void setGraphicWalletDetailsService(GraphicWalletDetailsService graphicWalletDetailsService) {
        this.graphicWalletDetailsService = graphicWalletDetailsService;
    }
}
